local class = require 'hump.class'
local HC = require 'hardoncollider'
require 'utilities.spacephysics'
require 'laser'
local state = require 'utilities.state'

--x, y = center of ship
Ship = class{
  function(self, color, x, y, collider, spacephysics)
    self.colour = color
    self.filename = 'images/' .. self.colour .. 'rocket.png'
    self.efilename = 'images/rocketexplosion.png'
    self.w = 14
    self.h = 24
    self.fire = {filename='images/fire.png', w=6, h=6, enabled=false}
    self.collider = collider
    self.rect = self.collider:addRectangle(x-self.w/2, y-self.h/2, self.w, self.h)
    self.rect.parent = self
    self.physics = spacephysics
    self.shipphysics = self.physics:newItem(10000, x, y)
    self.shipphysics:setMaximumSpeed(0)
    self.laser = Laser(self.collider, self.physics)
  end
}

function Ship:ready()
  if self.shipphysics then
    self.shipphysics:setMaximumSpeed(1000)
  end
end

function Ship:unready()
  if self.shipphysics then
    self.shipphysics:setMaximumSpeed(0)
  end
end

function Ship:isReady()
  local ret = true
  if self.shipphysics then
    ret = self.shipphysics:maximumSpeed() ~= 0
  end
  return ret
end

function Ship:color()
  return self.colour
end

function Ship:getComponents()
  local r = self.rect:rotation()
  return math.sin(r), -1*math.cos(r)
end

function Ship:update(dt)
  self.laser:update()
  if self.shipphysics then
    self.rect:moveTo(self.shipphysics:center())
  end
end

function Ship:accelerate(dt)
  if self:alive() then
    local ax, ay = self:getComponents()
    self.shipphysics:accelerate(ax*150, ay*150, dt)
    self.fire.enabled = true
  end
end

function Ship:alive()
  return self.shipphysics ~= nil
end

function Ship:velocity()
  local vx, vy = 0, 0
  if self.shipphysics then
    vx, vy = self.shipphysics:velocity()
  end
  return vx, vy
end

function Ship:center()
  return self.rect:center()
end

--angle is in radians
function Ship:rotate(angle, dt)
  if self:alive() then
    self.rect:rotate(angle*dt)
  end
end

function Ship:onCollide()
  self.collider:remove(self.rect)
  self.physics:removeItem(self.shipphysics)
  self.shipphysics = nil
  self.fire.enabled = false
  self.eaudio = 'audio/explosion2.wav'
end

function Ship:shoot(dt)
  if self:alive() then
    local cx, cy = self.rect:center()
    local compx, compy = self:getComponents()
    local collisionFudge=16 --offset laser enough to avoid collision detection
    cx = cx + compx * (self.h+collisionFudge)/2
    cy = cy + compy * (self.h+collisionFudge)/2
    self.laser:shoot(cx, cy, self.rect:rotation(), self.shipphysics:maximumSpeed())
  end
end

function Ship:rotation()
  return self.rect:rotation()
end

function Ship:firestate()
  if self.fire.enabled then
    local cx, cy = self.rect:center()
    local r = self:rotation()
    self.fire.enabled = false
    return state.new(cx-(self.h+self.fire.h)*math.sin(r)/2, cy+(self.h+self.fire.h)*math.cos(r)/2, self.fire.w, self.fire.h, r, self.fire.filename)
  end
end

function Ship:state()
  local cx, cy = self.rect:center()
  local w, h = self.w, self.h
  local f = self.filename
  if not self:alive() then
    f = self.efilename
    w = h
  end
  local shipstate = state.new(cx, cy, w, h, self:rotation(), f, self.eaudio)
  self.eaudio = nil
  local ret = {}
  ret[''] = shipstate
  ret[' laser'] = self.laser:state()
  ret[' fire'] = self:firestate()
  return ret
end
